package javashitang;

import vo.Person;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class lambda3 {

    //joining
    public static void test1(){
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 8900, 23, "male", "New York"));
        personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
        personList.add(new Person("Lily", 7800, 21, "female", "Washington"));

        String collect = personList.stream().map(Person::getName).collect(Collectors.joining(",", "[", "]"));

        Map<Integer, Person> collect1 = personList.stream().collect(Collectors.toMap(e -> e.getAge(), e -> e));
        collect1.forEach((k,v) ->{
            System.out.println(v);
        });

        System.out.println(collect);
    }

    public static void test2(){
        List<Person> personList = new ArrayList<Person>();

        personList.add(new Person("Sherry", 9000, 24, "female", "New York"));
        personList.add(new Person("Tom", 8900, 22, "male", "Washington"));
        personList.add(new Person("Jack", 9000, 25, "male", "Washington"));
        personList.add(new Person("Lily", 8800, 26, "male", "New York"));
        personList.add(new Person("Alisa", 9000, 26, "female", "New York"));

        //工资升序排序
        List<String> collect = personList.stream().sorted(Comparator.comparing(Person::getSalary)).map(Person::getName).
                collect(Collectors.toList());

        //降序
        List<String> collect1 = personList.stream().sorted(Comparator.comparing(Person::getSalary).reversed()).map(Person::getName).collect(Collectors.toList());


        //先按工资再按年龄升序排序
        List<String> collect2 = personList.stream().sorted(Comparator.comparing(Person::getAge).
                thenComparing(Person::getSalary)).map(Person::getName).collect(Collectors.toList());

        //先按工资再按年龄自定义排序（降序）
        List<String> collect3 = personList.stream().sorted((p1, p2) -> {
            if (p1.getSalary() == p2.getSalary()) {
                return p2.getAge() - p1.getAge();
            } else {
                return p2.getSalary() - p1.getSalary();
            }
        }).map(Person::getName).collect(Collectors.toList());

        System.out.println(collect2);
    }

    public static void test3(){
        List<String> stringList = Arrays.asList("a", "b", "A", "B");
        //stringList.sort((s1,s2) -> s1.compareToIgnoreCase(s2));
        stringList.sort(String::compareToIgnoreCase);

        stringList.forEach(x -> System.out.println(x));
    }

    public static void test4(){
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 8900, 23, "male", "New York"));
        personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
        personList.add(new Person("Lily", 7800, 21, "female", "Washington"));

        personList.stream().mapToInt(Person::getAge).sum();//和
        personList.stream().mapToInt(Person::getAge).max();//最大
        personList.stream().mapToInt(Person::getAge).average();//平均值

    }

    public static void main(String[] args) {
        test2();
    }
}
